package cn.itsource.controller;

import cn.itsource.domain.Goods;
import cn.itsource.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    //查询所有数据
    @RequestMapping("/list")
    public String list(Model model) {
        List<Goods> goodsList = goodsService.findAll();
        model.addAttribute("goodss", goodsList);
        return "goods/goodsList";
    }

    //根据id删除
    @RequestMapping("/delete")
    public String del(Integer id) {
        goodsService.delete(id);
        return "redirect:/goods/list";
    }

    //中间站，跳转
    @RequestMapping("/jump")
    public String jump(Integer id, Model model) {
        if (id != null) {
            Goods goodsServiceOne = goodsService.findOne(id);
            model.addAttribute("goods", goodsServiceOne);
            return "goods/goodsUpdate";
        }
        return "goods/goodsAdd";
    }

    //添加数据
    @RequestMapping("/add")
    public String add(Goods goods) {
        goodsService.add(goods);
        return "redirect:/goods/list";
    }

    //修改数据
    @RequestMapping("/update")
    public String update(Goods goods) {
        goodsService.update(goods);
        return "redirect:/goods/list";
    }


    //查看一条数据
    @RequestMapping("/view")
    public String view(Integer id, Model model) {
        Goods goodsServiceOne = goodsService.findOne(id);
        model.addAttribute("goods", goodsServiceOne);
        return "goods/goodsView";
    }
}
